package com.lee.member.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lee.http.SpringBeanContext;
import com.lee.member.api.service.UnionLoginService;
import com.lee.member.config.UnionLoginConfig;
import com.lee.member.entity.UnionLogin;
import com.lee.member.mapper.UnionLoginMapper;
import com.lee.member.strategy.UnionLoginStrategy;
import com.lee.response.ResponseResult;
import com.lee.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 联合登录实现
 * @author zfl_a
 * @date 2020/4/5
 * @project login-authorization-parent
 */
@RestController
public class UnionLoginServiceImpl implements UnionLoginService {

    @Autowired
    private TokenUtils tokenUtils ;
    @Autowired
    private UnionLoginMapper unionLoginMapper ;
    @Autowired
    private UnionLoginConfig unionLoginConfig ;

    @Override
    public ResponseResult unionLogin(@RequestParam("publicId") String publicId) {
        if (StringUtils.isEmpty(publicId)) {
            return new ResponseResult(400,"publicId不能够为空",null);
        }
        // 根据渠道id查询 联合基本信息
        UnionLogin unionLogin = unionLoginMapper.selectByUnionLoginId(publicId);
        if (unionLogin == null) {
            return new ResponseResult(400,"该渠道不存在或者已关闭,渠道id："+publicId,null);
        }
        //封装并返回相应数据
        String requestAddres = unionLogin.getRequestAddress() ;
        JSONObject resultData = new JSONObject();
        resultData.put("requestAddres", requestAddres);
        return new ResponseResult(200,"成功",resultData);
    }

    @Override
    public ResponseResult unionLoginCallback(@RequestParam("publicId") String publicId) {
        if (StringUtils.isEmpty(publicId)) {
            return new ResponseResult(400,"publicId不能够为空",null);
        }
        // 根据渠道id查询 联合基本信息
        UnionLogin unionLogin = unionLoginMapper.selectByUnionLoginId(publicId);
        if (unionLogin == null) {
            return new ResponseResult(400,"该渠道不存在或者已关闭,渠道id："+publicId,null);
        }
        String unionBeanId = unionLogin.getUnionBeanId();
        if (StringUtils.isEmpty(unionBeanId)) {
            return new ResponseResult(400,"系统参数错误",null);
        }
        //  从Spring容器中根据beanId查询对应的策略类
        UnionLoginStrategy unionLoginStrategy = SpringBeanContext.getBean(unionBeanId, UnionLoginStrategy.class);
        // 根据当前线程获取request对象
        HttpServletRequest request = ((ServletRequestAttributes)
                (RequestContextHolder.currentRequestAttributes())).getRequest();
        //调用对应策略类，拿到openId
        String openId = unionLoginStrategy.unionLoginCallback(request, unionLogin);
        if (StringUtils.isEmpty(openId)) {
            return new ResponseResult(400,"系统参数错误",null);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(unionLoginConfig.getOpenIdKey(), openId);
        jsonObject.put(unionLoginConfig.getPublicIdKey(), publicId);
        //token令牌，登录使用
        String token = tokenUtils.createToken(unionLoginConfig.getUnionTokenKeyPrefix(), jsonObject.toJSONString());
        JSONObject resultData = new JSONObject();
        resultData.put("token", token);
        return new ResponseResult(200,"成功",resultData);
    }
}
